﻿'Importamos los Espacios de Nombres que necesitamos en el modulo de importacion de Clases de las DLL del Sistema.
'Esta información es tomada del MSDN de Microsoft, de acuerdo con las especificaciones del
'How to Use WinUSB to Comunicate with a USB Device.

'Importamos las funciones propias del WinUSB.sys.
'Para más información:
'   http://msdn.microsoft.com/en-us/library/aa476437.aspx


Imports System
Imports System.Runtime.InteropServices
Imports System.IO
Imports Microsoft.Win32.SafeHandles

Namespace USBConverter
    Public Class MSDNImports

        '********************************************************************* CONSTANTES
        Friend Const DIGCF_PRESENT As Int32 = 2
        Friend Const DIGCF_DEVICEINTERFACE As Int32 = &H10
        Friend Const FILE_ATTRIBUTE_NORMAL As Int32 = &H80
        Friend Const FILE_FLAG_OVERLAPPED As Int32 = &H40000000
        Friend Const FILE_SHARE_READ As Int32 = 1
        Friend Const FILE_SHARE_WRITE As Int32 = 2
        Friend Const GENERIC_READ As UInt32 = &H80000000UL
        Friend Const GENERIC_WRITE As UInt32 = &H40000000
        Friend Const INVALID_HANDLE_VALUE As Int32 = -1
        Friend Const OPEN_EXISTING As Int32 = 3
        '********************************************************************* ESTRUCTURAS
        Public Structure SP_DEVICE_INTERFACE_DATA
            Friend cbSize As Int32
            Friend InterfaceClassGuid As Guid
            Friend Flags As Int32
            Friend Reserved As IntPtr
        End Structure
        Public Structure SP_DEVICE_INTERFACE_DETAIL_DATA
            Friend cbSize As Int32
            Friend DevicePath As String
        End Structure
        '********************************************************************* IMPORTACIÓN DESDE LAS DLL's del sistema
        <DllImport("setupapi.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
        Shared Function SetupDiGetClassDevs( _
            ByRef ClassGuid As Guid, _
            ByVal Enumerator As IntPtr, _
            ByVal hwndParent As IntPtr, _
            ByVal Flags As Int32) _
            As IntPtr
        End Function
        <DllImport("setupapi.dll", SetLastError:=True)> _
        Shared Function SetupDiEnumDeviceInterfaces( _
            ByVal DeviceInfoSet As IntPtr, _
            ByVal DeviceInfoData As IntPtr, _
            ByRef InterfaceClassGuid As Guid, _
            ByVal MemberIndex As Int32, _
            ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA) _
            As Boolean
        End Function
        <DllImport("setupapi.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
        Shared Function SetupDiGetDeviceInterfaceDetail( _
            ByVal DeviceInfoSet As IntPtr, _
            ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA, _
            ByVal DeviceInterfaceDetailData As IntPtr, _
            ByVal DeviceInterfaceDetailDataSize As Int32, _
            ByRef RequiredSize As Int32, _
            ByVal DeviceInfoData As IntPtr) _
            As Boolean
        End Function
        <DllImport("setupapi.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
        Shared Function SetupDiGetDeviceInterfaceDetail( _
            ByVal DeviceInfoSet As IntPtr, _
            ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA, _
            ByVal DeviceInterfaceDetailData As SP_DEVICE_INTERFACE_DETAIL_DATA, _
            ByVal DeviceInterfaceDetailDataSize As Int32, _
            ByRef RequiredSize As Int32, _
            ByVal DeviceInfoData As IntPtr) _
            As Boolean
        End Function
        <DllImport("setupapi.dll", SetLastError:=True)> _
        Shared Function SetupDiDestroyDeviceInfoList( _
            ByVal DeviceInfoSet As IntPtr) _
            As Int32
        End Function
        <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
        Shared Function CreateFile( _
            ByVal lpFileName As String, _
            ByVal dwDesiredAccess As UInt32, _
            ByVal dwShareMode As Int32, _
            ByVal lpSecurityAttributes As IntPtr, _
            ByVal dwCreationDisposition As Int32, _
            ByVal dwFlagsAndAttributes As Int32, _
            ByVal hTemplateFile As Int32) _
            As SafeFileHandle
        End Function
        <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
        Shared Function CloseHandle( _
            ByVal hObject As SafeFileHandle) As Boolean
        End Function
        '********************************************************************* IMPORTACIÓN DESDE LAS DLL's del WinUSB
        <DllImport("winusb.dll", SetLastError:=True)> _
        Friend Shared Function WinUsb_Free( _
            ByVal InterfaceHandle As IntPtr) _
            As Boolean
        End Function
        <DllImport("winusb.dll", SetLastError:=True)> _
        Friend Shared Function WinUsb_Initialize( _
            ByVal DeviceHandle As SafeFileHandle, _
            ByRef InterfaceHandle As IntPtr) _
            As Boolean
        End Function
        <DllImport("winusb.dll", SetLastError:=True)> _
        Friend Shared Function WinUsb_ReadPipe( _
            ByVal InterfaceHandle As IntPtr, _
            ByVal PipeID As Byte, _
            ByVal Buffer() As Byte, _
            ByVal BufferLength As UInt32, _
            ByRef LengthTransferred As UInt32, _
            ByVal Overlapped As IntPtr) _
            As Boolean
        End Function
        <DllImport("winusb.dll", SetLastError:=True)> _
        Friend Shared Function WinUsb_WritePipe( _
            ByVal InterfaceHandle As IntPtr, _
            ByVal PipeID As Byte, _
            ByVal Buffer() As Byte, _
            ByVal BufferLength As UInt32, _
            ByRef LengthTransferred As UInt32, _
            ByVal Overlapped As IntPtr) _
            As Boolean
        End Function
        <DllImport("winusb.dll", SetLastError:=True)> _
            Friend Shared Function WinUsb_FlushPipe( _
            ByVal InterfaceHandle As IntPtr, _
            ByVal PipeID As Byte) _
            As Boolean
        End Function
        <DllImport("winusb.dll", SetLastError:=True)> _
            Friend Shared Function WinUsb_ResetPipe( _
            ByVal InterfaceHandle As IntPtr, _
            ByVal PipeID As Byte) _
            As Boolean
        End Function

    End Class
End Namespace